package fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.Response;

import org.json.JSONException;
import org.json.JSONObject;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import utils.CarServerLoginDB;
import utils.CircleNetworkImage;
import utils.MyStoreDao;
import utils.MyUrl;
import utils.RequestQueueUtils;
import zhang.automobileservice.ExitLoginActivity;
import zhang.automobileservice.MyLoginActivity;
import zhang.automobileservice.MycarActivity;
import zhang.automobileservice.MypublishActivity;
import zhang.automobileservice.MyrecommnedActivity;
import zhang.automobileservice.MysettingActivity;
import zhang.automobileservice.MystoreActivity;
import zhang.automobileservice.R;

/**
 * Created by Administrator on 2016/7/18.
 */
public class MyselfFragment extends Fragment implements View.OnClickListener {
    private CircleNetworkImage tit_picture;
    private ImageView im_carmyself;
    private ImageView im_carmystore;
    private ImageView im_carmypublish;
    private ImageView im_carmyrecommend;
    private ImageView im_carsetting;
    private Button bt_myself;
    private RelativeLayout rl_car;
    private RelativeLayout rl_restore;
    private RelativeLayout rl_publish;
    private RelativeLayout rl_recommend;
    private RelativeLayout rl_setting;
    private String id;
    private SharedPreferences preferences;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_myself, null);
        ShareSDK.initSDK(getActivity(),"1592856a5b1cf");
        initView(view);
        preferences=getActivity().getSharedPreferences("TAGtext", Context.MODE_PRIVATE);
        id = preferences.getString("id","1");
        /*CarServerLoginDB db = new CarServerLoginDB(getActivity());
        if(db.queryId()!=null){
            id = db.queryId().getId();
        }*/

        return view;
    }

    @Override
    public void onResume(){
        super.onResume();
        if(id==null){
            bt_myself.setText("点击登录");
            RequestQueueUtils.exectueCircleNetWorkImageView(getActivity(),MyUrl.Font_Url+MyUrl.Image+"default.jpg",tit_picture);
        }else {
            String url = MyUrl.Font_Url+MyUrl.getData+"id="+id;
            RequestQueueUtils.exectueJsonRequest(getContext(), url, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject o) {
                    try {
                        JSONObject object ;
                        if("0".equals(o.getString("code"))){
                            object = o.getJSONObject("data");
                            String userName = object.getString("userName");
                            String headPic = object.getString("headPic");
                            Log.e("----",userName+"++++"+headPic);
                            RequestQueueUtils.exectueCircleNetWorkImageView(getActivity(),MyUrl.Font_Url+MyUrl.Image+headPic,tit_picture);
                            bt_myself.setText(userName);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_myself:
                Intent intent0 = new Intent(getActivity(), MyLoginActivity.class);
                startActivity(intent0);
                break;
            case R.id.rl_car:
                Intent intent = new Intent(getActivity(), MycarActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_restore:
                Intent intent1 = new Intent(getActivity(), MystoreActivity.class);
                startActivity(intent1);
                break;
            case R.id.rl_publish:
                Intent intent2 = new Intent(getActivity(), MypublishActivity.class);
                startActivity(intent2);
                break;
            case R.id.rl_recommend:
                showShare();
                /*Intent intent3 = new Intent(getActivity(), MyrecommnedActivity.class);
                startActivity(intent3);*/
                break;
            case R.id.rl_setting:
                Intent intent4 = new Intent(getActivity(), MysettingActivity.class);
                startActivity(intent4);
                break;
            case R.id.tit_picture:
                Intent intent5 = new Intent(getActivity(), ExitLoginActivity.class);
                startActivity(intent5);
                break;
        }
    }

    private void initView(View view) {
        tit_picture = (CircleNetworkImage) view.findViewById(R.id.tit_picture);
        tit_picture.setOnClickListener(this);
        im_carmyself = (ImageView) view.findViewById(R.id.im_carmyself);
        im_carmystore = (ImageView) view.findViewById(R.id.im_carmystore);
        im_carmypublish = (ImageView) view.findViewById(R.id.im_carmypublish);
        im_carmyrecommend = (ImageView) view.findViewById(R.id.im_carmyrecommend);
        im_carsetting = (ImageView) view.findViewById(R.id.im_carsetting);
        bt_myself = (Button) view.findViewById(R.id.bt_myself);

        bt_myself.setOnClickListener(this);
        im_carmyself.setOnClickListener(this);
        im_carmystore.setOnClickListener(this);
        im_carmypublish.setOnClickListener(this);
        im_carmyrecommend.setOnClickListener(this);
        im_carsetting.setOnClickListener(this);
        rl_car = (RelativeLayout) view.findViewById(R.id.rl_car);
        rl_car.setOnClickListener(this);
        rl_restore = (RelativeLayout) view.findViewById(R.id.rl_restore);
        rl_restore.setOnClickListener(this);
        rl_publish = (RelativeLayout) view.findViewById(R.id.rl_publish);
        rl_publish.setOnClickListener(this);
        rl_recommend = (RelativeLayout) view.findViewById(R.id.rl_recommend);
        rl_recommend.setOnClickListener(this);
        rl_setting = (RelativeLayout) view.findViewById(R.id.rl_setting);
        rl_setting.setOnClickListener(this);
    }

    private void showShare() {
        ShareSDK.initSDK(getActivity());
        OnekeyShare oks = new OnekeyShare();
//关闭sso授权
        oks.disableSSOWhenAuthorize();

// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
// titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://sharesdk.cn");
// text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
// url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
// comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
// site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
// siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(getActivity());
    }

}
